﻿using AjaxControlToolkit;
using ModernShopping.AppCode;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ModernShopping.User
{
    public partial class MyStores : System.Web.UI.Page
    {
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            LoginInfo loginInfo = Session.GetLoginInfo();
            if (loginInfo == null)
            {
                Response.GoToAccessDeniedPage(Request);
            }
            if (!loginInfo.IsUser())
            {
                Response.GoToAccessDeniedPage(Request);
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillGridView();
            }
        }

        private void FillGridView()
        {
            LoginInfo info = Session.GetLoginInfo();
            Guid UserID = info.UserID;
            DbStore.InitGridView(gvMyStores, UserID);
            lblTongSoCuaHang.Text = string.Format("Công ty bạn hiện có {0} cửa hàng.", DbStore.getNumberOfStores(UserID));
        }

        protected void btnClose_Click(object sender, ImageClickEventArgs e)
        {
            FillGridView();
            PopupUpdate.Hide();
        }

        protected void lbSelect_Click(object sender, EventArgs e)
        {
            LinkButton lb = (sender as LinkButton);
            int StoreID = int.Parse(lb.CommandArgument);
            iFrame.Attributes["src"] = string.Format("GoogleMapUpdate.aspx?SelectedStore={0}", StoreID);
            PopupUpdate.Show();
        }

        protected void btnCloseAdd_Click(object sender, ImageClickEventArgs e)
        {
            FillGridView();
            PopupAdd.Hide();
        }

        protected void lbDelete_Click(object sender, EventArgs e)
        {

        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            ModernShoppingDataContext db = new ModernShoppingDataContext();
            foreach (GridViewRow row in gvMyStores.Rows)
            {
                CheckBox cb = row.FindControl("cbDelete") as CheckBox;
                HiddenField hf = row.FindControl("hfCode") as HiddenField;
                if (cb.Checked == true)
                {
                    int CodeStore=int.Parse(hf.Value);
                    Store tmp = db.Stores.Where(c => c.Code.Equals(CodeStore)).FirstOrDefault();
                    tmp.isDeleted = true;
                }
            }
            db.SubmitChanges();
            FillGridView();
        }
    }
}